<?php
 include_once('config/config.php');
 // 获取id
 // id
 $id = $_GET['id'];
 // 查询单条数据
 $sql = "SELECT pre_person.*, pre_job.name as jobname, pre_department.name as depname FROM pre_person LEFT JOIN pre_job ON pre_person.jobid = pre_job.id LEFT JOIN pre_department ON pre_person.depid = pre_department.id WHERE pre_person.id = '$id';";
 
 $person = find($sql);
 // 查询所有部门
 $DepData = all("SELECT * FROM pre_department;");
 
 // 查询所有职位
 $JobData = all("SELECT * FROM pre_job;");
 // pre($person);
 // exit;

 // 判断有没有表单提交过来
 if($_POST){
	// 接收全部参数
	    $params = $_POST;
		
		// 查询部门职位
		if($_POST['action'] == 'depid'){
			// 获取id
			$depid = $_POST['depid'];
			// 查询本身
			$JobData  = all("SELECT * FROM pre_job WHERE depid = '$depid';");
			// 判断是否为空  为空可以使用
			
			if(!empty($JobData)){
				success('查询成功',$JobData);
			}else{
				error('查询失败');
			}
		}
		
		//
		$params = $_POST;
		// 封装更新数组
		$data = [
		  'name' => $params['name'],
		  'sex' => $params['sex'],
		  'mobile' => $params['mobile'],
		  'email' => $params['email'],
		  'address' => $params['address'],
		  'depid' => $params['depid'],
		  'jobid' => $params['jobid'],
		];
			
		
	    // 判断是否有文件上传且大小
	    if($_FILES['avatar'] && $_FILES['avatar']['size'] > 0)
	    {
	      // 调用上传函数
	      $avatar = upload('avatar');
	
	      if($avatar['code'] == 0)
	      {
	        ShowMsg($avatar['msg']);
	        exit;
	      }else{
	        $data['avatar'] = $avatar['data'];
	      }
	    }
		
		
	    $result = update('person',$data,"`id` = '$id'");
	
	    if($result)
	    {
	      if(isset($data['avatar']) && $_FILES['avatar']['size'] > 0)
	      {
	        @is_file($person['avatar']) && @unlink($person['avatar']);
	      }
	
	      ShowMsg('更新成功','index.php');
	      exit;
	    }else{
	      
	      @is_file($data['avatar']) && @unlink($data['avatar']);
	
	      ShowMsg('未修改','index.php');
	      exit;
	    }
 }

?>
<!DOCTYPE html>
<html lang="en">
	<head>
		<?php include_once('common/meta.php'); ?>
	</head>
	<style>
		.avatar{
			width: 200px;
			height: 200px;
			display: inline-block;
			user-select: none;
			cursor: pointer;
		}
		.avatar img{
			width: 200px;
			height: 200px;
		}
		
	</style>
	<body>

		<?php include_once('common/header.php'); ?>

		<?php include_once('common/sidebar.php'); ?>

		<div class="content">
			<div class="header">
				<h1 class="page-title">编辑用户</h1>
			</div>
			<ul class="breadcrumb">
				<li><a href="index.php">Home</a> <span class="divider">/</span></li>
				<li class="active">Person</li>
			</ul>
			<div class="container-fluid">
				<div class="row-fluid">

					<div class="btn-toolbar">
						<button class="btn btn-primary" onClick="location='index.php'"><i class="icon-list"></i>管理员列表</button>
						<div class="btn-group">
						</div>
					</div>

					<div class="well">
						<div id="myTabContent" class="tab-content">
							<div class="tab-pane active in" id="home">
								<form method="post" enctype="multipart/form-data">
								  <!-- 用户名 -->
								  <label>员工姓名</label>
								  <input type="text" value="<?php echo $person['name'] ?>" class="input-xxlarge" name="name" placeholder="请输入员工姓名" required>
								  
								  <!-- 手机号 -->
								  <label>手机号</label>
								  <input type="text" value="<?php echo $person['mobile'] ?>" maxlength="11" oninput="value=value.replace(/[^\d]/g,'')" class="input-xxlarge" name="mobile" placeholder="请输入手机号" required>
								  <span id="mobile"></span>
								
								  <!-- 邮箱 -->
								  <label>邮箱</label>
								  <input type="text" value="<?php echo $person['email'] ?>" class="input-xxlarge" name="email" placeholder="请输入邮箱" required>
							      <span id="email"></span>
								
								  <!-- 性别 -->
								  <label>性别</label>
								  <input type="radio" name="sex" id="Radio0" value="0" <?php echo $person['sex'] == 0 ? 'checked' : ' '; ?>> <label style="display: inline-block;" for="Radio0">保密</label>
								  <input type="radio" name="sex" id="Radio1" value="1"  <?php echo $person['sex'] == 1 ? 'checked' : ' '; ?>><label style="display: inline-block;" for="Radio1">男</label>
								  <input type="radio" name="sex" id="Radio2" value="2"  <?php echo $person['sex'] == 2 ? 'checked' : ' '; ?>> <label style="display: inline-block;" for="Radio2">女</label>
								
								  <!-- 地址 -->
								  <label>地址</label>
								  <input type="text" class="input-xxlarge" name="address" placeholder="请输入地址" value="<?php echo $person['address'] ?>" required>
								
								  <!-- 所属部门 -->
								  <label>所属部门</label>
								  <select name="depid" id="depid">
								    <option value="0">请选择</option>
								    <?php foreach($DepData as $item){ ?>
								      <option value="<?php echo $item['id'] ?>" <?php echo $person['depid'] == $item['id'] ? 'selected' : ' '; ?> ><?php echo $item['name'] ?></option>
								    <?php } ?>
								  </select>
								  
								  <!-- 所属职位 -->
								  <label>所属职位</label>
								  <select name="jobid" id="jobid">
								    <option value="0">请选择</option>
									<?php foreach($JobData as $item){ ?>
									  <option value="<?php echo $item['id'] ?>" <?php echo $person['jobid'] == $item['id'] ? 'selected' : ' '; ?> ><?php echo $item['name'] ?></option>
									<?php } ?>
								  </select>
								  
								  <!-- 头像 -->
								  <label>头像</label>
								  <input type="file" name="avatar" id="avatar" hidden >
								  <label class="avatar" for="avatar">
								    <img src="<?php echo $person['avatar'] ?>" alt="" id="img">
								  </label>
								
								  <label></label>
								  <input class="btn btn-primary" type="submit" value="提交" />
								</form>
							</div>
						</div>
					</div>
					<?php include_once('common/footer.php'); ?>

				</div>
			</div>
		</div>

		<?php include_once('common/script.php'); ?>

	</body>
</html>
<script>
  // 图片预览函数
  function GetObjectURL(file) {
    // createObjectURL  给一个文件对象 可以提取出一个url本地地址出来
    var url = null;

    //判断是否为IE浏览器还是google还是其他浏览器
    if (window.createObjectURL != undefined) {
      url = window.createObjectURL(file)
    } else if (window.URL != undefined) {
      url = window.URL.createObjectURL(file)
    } else if (window.webkitURL != undefined) {
      url = window.webkitURL.createObjectURL(file)
    }

    return url
  }

  $('#avatar').change(function(){

    // 获取选中的文件
    let file = $(this)[0].files[0] ? $(this)[0].files[0] : {};
    
    // 获取文件地址
    let url = GetObjectURL(file)

    // 赋值给img
    $('#img').attr('src',url)

  })
  
  $('#depid').change(function(){
    // 获取部门id
    let val = $(this).val()
  
    // 发起请求
    $.ajax({
      type:'post',
      url:'Personup.php',
      data:{
        depid:val,
        action:'depid'
      },
      dataType:'json',
      success:function(result)
      {
        if(result.code === 1)
        {
          let option = ''
  
          result.data.forEach((item) => {
            option += `<option value="${item.id}">${item.name}</option>`
          });
          $('#jobid').html(option)
        }
      }
    })
  })
  
</script>